package stardust.product.xspider;


import stardust.core.java.thread.FixedThreadPool;


public class XSpider
{

  private int threadCount;
  private FixedThreadPool threadPool;
  
  private XSpiderCallback callback;
  
  public XSpider(XSpiderCallback callback, int threadCount)
  {
    this.callback = callback;
    this.threadCount = threadCount;
  }
  
  public void start() throws InterruptedException
  {
    this.stop();
    
    this.threadPool = new FixedThreadPool("SpiderThread", this.threadCount, this.threadCount);
    
    for (int i = 0; i < this.threadCount; i++)
    {
      this.threadPool.run(new XSpiderProcessor(XSpider.this.callback));
    }
  }
  
  public void join() throws InterruptedException
  {
    synchronized (this.threadPool)
    {
      this.threadPool.wait();
    }
  }
  
  public void stop() throws InterruptedException
  {
    if (this.threadPool != null)
    {
      this.threadPool.shutdown();
      this.threadPool = null;
    }
  }

}
